package cn.zc.curriculum.service.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;


import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * 多线程配置类
 */

@Configuration
@EnableAsync
public class AsyncExecutorConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程数据，初始化创建线程数
        executor.setCorePoolSize(15);
        //最大线程数，线程池中最大线程
        executor.setMaxPoolSize(25);
        //缓冲队列，用于缓冲执行任务的队列
        executor.setQueueCapacity(500);
        //允许空闲时间60秒，当超过核心线程数的线程，超过60秒空闲则进行销毁
        executor.setKeepAliveSeconds(60);
        //线程前缀
        executor.setThreadNamePrefix("Async-zc-");
        //线程策略，当缓冲队列满以后，进行拒绝，由主线程进行处理任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        executor.initialize();
        return  executor;
    }
}
